"""
用户相关数据模型
"""
from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Enum, Text, ForeignKey, DECIMAL
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base

class User(Base):
    __tablename__ = "users"
    
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, nullable=False, index=True)
    nickname = Column(String(50), nullable=True)
    email = Column(String(100), unique=True, nullable=False, index=True)
    password_hash = Column(String(255), nullable=False)
    avatar_url = Column(Text, nullable=True)
    is_verified = Column(Boolean, default=False)
    email_verification_token = Column(String(128), nullable=True)
    email_verified_at = Column(DateTime, nullable=True)
    status = Column(Enum('active', 'inactive', 'banned'), default='active', index=True)
    role = Column(Enum('user', 'admin', 'super_admin'), default='user')
    balance = Column(DECIMAL(10, 2), default=0.00)
    last_login_at = Column(DateTime, nullable=True)
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    
    # 关系
    login_logs = relationship("LoginLog", back_populates="user")
    reputation = relationship("UserReputation", back_populates="user", uselist=False)
    wallet = relationship("Wallet", back_populates="user", uselist=False)
    products = relationship("Product", back_populates="user")
    product_views = relationship("ProductView", back_populates="user")
    favorites = relationship("Favorite", back_populates="user")
    trades_as_buyer = relationship("Trade", foreign_keys="[Trade.buyer_id]", back_populates="buyer")
    trades_as_seller = relationship("Trade", foreign_keys="[Trade.seller_id]", back_populates="seller")

class LoginLog(Base):
    __tablename__ = "login_logs"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    ip_address = Column(String(45), nullable=True)
    user_agent = Column(Text, nullable=True)
    login_time = Column(DateTime, default=func.now())
    status = Column(Enum('success', 'failed'), nullable=False)
    failure_reason = Column(String(255), nullable=True)
    
    # 关系
    user = relationship("User", back_populates="login_logs")

class UserReputation(Base):
    __tablename__ = "user_reputation"
    
    user_id = Column(Integer, ForeignKey("users.id"), primary_key=True)
    total_evaluations = Column(Integer, default=0)
    positive_evaluations = Column(Integer, default=0)
    negative_evaluations = Column(Integer, default=0)
    average_score = Column(DECIMAL(3, 2), default=0.00)
    product_quality_avg = Column(DECIMAL(3, 2), default=0.00)
    service_attitude_avg = Column(DECIMAL(3, 2), default=0.00)
    logistics_speed_avg = Column(DECIMAL(3, 2), default=0.00)
    reputation_score = Column(Integer, default=1000)
    reputation_level = Column(Enum('bronze', 'silver', 'gold', 'platinum', 'diamond'), default='bronze')
    badges = Column(Text, nullable=True)  # JSON格式存储
    is_verified_seller = Column(Boolean, default=False)
    last_updated = Column(DateTime, default=func.now(), onupdate=func.now())
    
    # 关系
    user = relationship("User", back_populates="reputation")

class Wallet(Base):
    __tablename__ = "wallets"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), unique=True, nullable=False)
    balance = Column(DECIMAL(10, 2), default=0.00)
    frozen_balance = Column(DECIMAL(10, 2), default=0.00)
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    
    # 关系
    user = relationship("User", back_populates="wallet")
    transactions = relationship("Transaction", back_populates="wallet")